package com.lnking.lnkingplace.service.impl;

import com.lnking.lnkingplace.Util.StringUtil;
import com.lnking.lnkingplace.model.dto.exhibition.ExhibitionImport;
import com.lnking.lnkingplace.model.dto.fourEnterprise.FourEnterpriseImport;
import com.lnking.lnkingplace.model.entity.Exhibition;
import com.lnking.lnkingplace.mapper.ExhibitionMapper;
import com.lnking.lnkingplace.model.entity.FourEnterprise;
import com.lnking.lnkingplace.model.request.exhibition.ExhibitionAddRequest;
import com.lnking.lnkingplace.model.request.exhibition.ExhibitionUpdateRequest;
import com.lnking.lnkingplace.service.IExhibitionService;
import com.lnking.starter.export.utils.ImportOption;
import com.lnking.starter.export.utils.ImportUtils;
import com.lnking.starter.mybatis.service.impl.CustomServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhangqt
 * @since 2022-09-18
 */
@Service
public class ExhibitionServiceImpl extends CustomServiceImpl<ExhibitionMapper, Exhibition> implements IExhibitionService {

    @Override
    public void add(ExhibitionAddRequest params) {
        Exhibition info = new Exhibition();
        BeanUtils.copyProperties(params,info);
        info.setCreateTime(LocalDateTime.now());
        this.baseMapper.insert(info);
    }

    @Override
    public void update(ExhibitionUpdateRequest params) {
        Exhibition info = new Exhibition();
        BeanUtils.copyProperties(params,info);
        this.baseMapper.updateById(info);
    }

    @Override
    public void importExcel(MultipartFile file) throws Exception {
        ImportOption option = new ImportOption.Builder().build();
        List<ExhibitionImport> result =  ImportUtils.importExcel(file, ExhibitionImport.class,option);
        for (ExhibitionImport exhibitionImport : result) {
            if (StringUtils.isNotBlank(exhibitionImport.getActivityName())) {
                Exhibition info = new Exhibition();
                BeanUtils.copyProperties(exhibitionImport,info);
                info.setCreateTime(LocalDateTime.now());

                if(StringUtils.isNotBlank(exhibitionImport.getStartTime())){
                    info.setStartTime(HSSFDateUtil.getJavaDate(Double.parseDouble(exhibitionImport.getStartTime())).toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime());
                }

                if(StringUtils.isNotBlank(exhibitionImport.getEndTime())){
                    info.setEndTime(HSSFDateUtil.getJavaDate(Double.parseDouble(exhibitionImport.getEndTime())).toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime());
                }


                if(StringUtils.isNotBlank(exhibitionImport.getStrIncome())){
                    info.setIncome(Float.valueOf(exhibitionImport.getStrIncome()));
                }
                if(StringUtils.isNotBlank(exhibitionImport.getStrInvestmentProjectAmount())){
                    info.setInvestmentProjectAmount(Float.valueOf(exhibitionImport.getStrInvestmentProjectAmount()));
                }
                if(StringUtils.isNotBlank(exhibitionImport.getStrVolumeOfTransaction())) {
                    info.setVolumeOfTransaction(Float.valueOf(exhibitionImport.getStrVolumeOfTransaction()));
                }
                if(StringUtils.isNotBlank(exhibitionImport.getStandNumber())){
                    info.setStandNumber(Integer.valueOf(exhibitionImport.getStandNumber()));
                }

                if(StringUtils.isNotBlank(exhibitionImport.getInvestmentProjectNumber())){
                    info.setInvestmentProjectNumber(Integer.valueOf(exhibitionImport.getInvestmentProjectNumber()));
                }

                this.baseMapper.insert(info);
            }

        }
    }
}
